package com.codefish;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 简单图形面积计算服务代理类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/06/03 下午 06:49
 */
public class ProxyAreaService implements IAreaService {

    /**
     * 原始服务实例
     */
    IAreaService areaService = new AreaService();
    /**
     * 自定义log对象
     */
    private static final MyLogger log = new MyLogger(ProxyAreaService.class);

    @Override
    public double countTriangleArea(double bottom, double height) {
        //输入参数合法性检查
        if (bottom <= 0 || height <= 0) {
            throw new IllegalArgumentException("不合法的参数");
        }
        //服务调用日志记录
        log.info(String.format("计算三角形面积,底为%.2f,高为%.2f", bottom, height));
        //调用被代理服务，计算面积
        double result = areaService.countTriangleArea(bottom, height);
        //对计算结果保留两位小数
        result = Double.parseDouble(String.format("%.2f", result));
        //日志打印计算结果
        log.info(String.format("三角形面积计算结果为%.2f", result));
        //返回结果
        return result;
    }


    /**
     * 自定义简单日志类
     */
    private static class MyLogger {
        Class<?> clazz;
        private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        public MyLogger(Class<?> clazz) {
            this.clazz = clazz;
        }

        public void info(String msg) {
            System.out.println(clazz.getName() + " " + SDF.format(new Date(System.currentTimeMillis())) + "[INFO]：" + msg);
        }
    }
}
